import pandas as pd
import numpy as np
from scipy import sparse as ssp
from sklearn.model_selection import KFold
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Input, Embedding, LSTM, Dense,Flatten, Dropout, merge,Convolution1D,MaxPooling1D,Lambda
from keras.layers.advanced_activations import PReLU,LeakyReLU,ELU
from keras.models import Model
import h5py
import os
from sklearn.preprocessing import StandardScaler


os.environ["CUDA_VISIBLE_DEVICES"] = "2"
seed = 1024
np.random.seed(seed)
path = '../data/'


train_ot = pd.read_pickle(path + 'train_X.pkl')
valid_ot = pd.read_pickle(path + 'valid_X.pkl')
dev_ot = pd.read_pickle(path + 'dev_X.pkl')


st = StandardScaler()
st.fit(train_ot)

train_ot = st.transform(train_ot)
valid_ot = st.transform(valid_ot)
dev_ot = st.transform(dev_ot)


print('success...')



def MLP():
    x = Input(shape=(136,), name='input')

    fc1 = Dense(512)(x)
    fc1 = PReLU()(fc1)
    fc1 = Dropout(0.3)(fc1)
    fc1 = Dense(512)(fc1)
    fc1 = PReLU()(fc1)
    fc1 = Dropout(0.3)(fc1)
    fc1 = Dense(128)(fc1)
    fc1 = PReLU()(fc1)
    fc1 = Dropout(0.2)(fc1)
    output_1 = Dense(1, activation='sigmoid')(fc1)
    model = Model(input=[x], output=[output_1])
    model.compile(
        optimizer='adam',
        loss='binary_crossentropy',
    )
    return model


# model_mlp=MLP()
path = '../data/'
# path = '../data/'
y = pd.read_pickle(path + 'train.pkl')['label']
y_va = pd.read_pickle(path+'valid.pkl')['label']
y_te = pd.read_pickle(path+'dev.pkl')['label']


res = np.zeros((y_va.shape[0], 1))
best_it = 23
fold = 1
skf = KFold(n_splits=5, shuffle=True, random_state=seed).split(y)

for ind_tr, ind_te in skf:
    X_ot_train = train_ot[ind_tr]
    X_ot_test = train_ot[ind_te]

    y_train = y[ind_tr]
    y_test = y[ind_te]
    # break

    model_mlp = MLP()
    model_mlp.fit(X_ot_train, y_train, batch_size=128, nb_epoch=8, verbose=2,
                  validation_data=[X_ot_test, y_test], shuffle=True)
    tmp_res = model_mlp.predict(valid_ot)
    print(tmp_res.shape, res.shape)
    res += tmp_res
    print('end fold:{}'.format(fold))
    fold += 1


print('end bagging')
res = res / 5.0

from sklearn.metrics import accuracy_score
y_v = (res+0.5).astype(int)
acc =  accuracy_score(y_va,y_v)

print('mlp model the accuracy on the valid set is : {}%'.format(round(acc* 100,2)))


